package com.zhengtoudai.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.zhengtoudai.R;
import com.zhengtoudai.base.MvpActivity;
import com.zhengtoudai.ui.bean.ShareRecordModel;
import com.zhengtoudai.ui.adapter.MyShareRecordAdapter;
import com.zhengtoudai.ui.presenter.ShareRecordPresenter;
import com.zhengtoudai.ui.view.ShareRecordView;
import com.zhengtoudai.util.AbToastUtil;
import com.zhengtoudai.view.recyclerview.MyDecoration;
import com.zhengtoudai.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.zhengtoudai.view.swipetoloadlayout.base.OnRefreshListener;
import com.zhengtoudai.view.swipetoloadlayout.base.SwipeToLoadLayout;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.zhengtoudai.R.id.refresh;

/**
 * 我的分享记录
 */
public class MyShareRecordActivity extends MvpActivity<ShareRecordPresenter> implements ShareRecordView, OnRefreshListener,OnLoadMoreListener {

    @Bind(R.id.swipe_target)
    RecyclerView mSwipeTarget;
    @Bind(refresh)
    SwipeToLoadLayout mRefresh;

    private int pageNum;
    private int totalNum;

    private MyShareRecordAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_share_record);
        ButterKnife.bind(this);
    }

    @Override
    protected ShareRecordPresenter createPresenter() {
        return new ShareRecordPresenter(this);
    }

    @Override
    protected void initTitle() {
        titleUtil.setTitle("邀请记录");
    }

    @Override
    protected void initView() {
        mRefresh.setOnRefreshListener(this);
        mRefresh.setOnLoadMoreListener(this);
        mSwipeTarget.setLayoutManager(new LinearLayoutManager(context));
        mSwipeTarget.addItemDecoration(new MyDecoration(this,MyDecoration.VERTICAL_LIST));
        mAdapter=new MyShareRecordAdapter();
        mSwipeTarget.setAdapter(mAdapter);
        mRefresh.post(new Runnable() {
            @Override
            public void run() {
                mRefresh.setRefreshing(true);
            }
        });
        autoLoading(mRefresh, mSwipeTarget);
    }

    @Override
    public void onLoadMore() {
        if(pageNum<totalNum){
            int current=pageNum+1;
            mvpPresenter.loadData(context,"10",current+"");
        }else{
            mRefresh.setLoadingMore(false);
            AbToastUtil.showToast(context,"已无更多数据");
        }
    }

    @Override
    public void onRefresh() {
        mAdapter.clearData();
        mvpPresenter.loadData(context,"10","1");
    }

    @Override
    public void getDataSuccess(ShareRecordModel model) {
        AbToastUtil.showToast(context,model.getMessage());
        ShareRecordModel.ShareRecordBean mInfo=model.getResult();
        if (mInfo.getRecommedList()==null||mInfo.getRecommedList().size()==0){
            status.showNoData("暂无数据");
        }else{
            status.removeView();
            totalNum=Integer.valueOf(mInfo.getPageInfo().getTotalNum());
            pageNum=Integer.valueOf(mInfo.getPageInfo().getPageNum());
            mAdapter.addData(mInfo.getRecommedList());
        }
    }

    @Override
    public void getDataFail(int code) {
        if (mRefresh.isRefreshing()){
            mRefresh.setRefreshing(false);
        }else{
            mRefresh.setLoadingMore(false);
        }
    }

    @Override
    public void showLoading() {
        if (mRefresh.isRefreshing()){
            mRefresh.setRefreshing(false);
        }else{
            mRefresh.setLoadingMore(false);
        }
    }

    @Override
    public void hideLoading() {

    }
}
